﻿using System;
using System.IO;
using TripleA.Core.Interfaces.Entities;
using TripleA.Core.Interfaces.Exceptions;

namespace TripleA.Core.Extensions.Registry
{
    public class RegistryKeyValueStep<T> : StepBase<T>
        where T: Context
    {
        private readonly string _fullPathWithItemName;
        private readonly string _expectedValue;

        public RegistryKeyValueStep(string fullPathWithItemName, string expectedValue)
        {
            _fullPathWithItemName = fullPathWithItemName;
            _expectedValue = expectedValue;
        }

        public override void Execute(T context)
        {
            var path = Path.GetDirectoryName(_fullPathWithItemName) ?? string.Empty;
            var item = Path.GetFileName(_fullPathWithItemName);
            var value = Microsoft.Win32.Registry.GetValue(path, item, null);

            if (value == null)
                throw new ExpectationException("Registry item '{0}' does not exist", _fullPathWithItemName);
            if (!value.ToString().Equals(_expectedValue, StringComparison.Ordinal))
                throw new ExpectationException("Registry item '{0}' has value '{1}'; expected '{2}'", 
                    _fullPathWithItemName, value, _expectedValue);
        }
    }
}